package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.OverScroller;
import android.widget.Scroller;

/**
 * Created by sunsg on 16/10/26.
 */
public class ScaleMoneyTest extends View{
    /**两个值之间的像素点*/
    private static final int DEFAULT_SAPCE = 13;
    private static final int SHOT_VERTICAL_LINE = 15;
    private static final int LONG_VERTICAL_LINE = 30;
    private Paint mLinePaint;
    private Paint mValuePaint;
    private Paint mVerticalPaint;     //垂直的线的画笔
    private Paint textPaint;

    /**速度追踪器*/
    private VelocityTracker mVelocityTracker;
    private OverScroller mScroller;

    private int defaultValue;
    private int moveX, lastMoveX = 0;
    private boolean isDrawText;
    private int lastX;
    private int lastCurrentMoveX;
    private boolean isLeft = false;
    private int mStartX = 0, mStartY, mStopX, mFirstX = 0;
    private int mWidth;


    private int mLastX;
    private int mLastY;

    private int mScrollX;
    private int mScrollY;
    private int mOverflingDistance;

    public ScaleMoneyTest(Context context) {
        this(context,null);
    }

    public ScaleMoneyTest(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScaleMoneyTest(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        mScroller = new OverScroller(context);
        mVelocityTracker = VelocityTracker.obtain();

        final ViewConfiguration configuration = ViewConfiguration.get(context);
//        mTouchSlop = configuration.getScaledTouchSlop();
//        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
//        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
//        mOverscrollDistance = configuration.getScaledOverscrollDistance();
        mOverflingDistance = configuration.getScaledOverflingDistance();

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(2);
        mLinePaint.setColor(0xFF999999);

        mVerticalPaint = new Paint();
        mVerticalPaint.setAntiAlias(true);
        mVerticalPaint.setStyle(Paint.Style.STROKE);
        mVerticalPaint.setStrokeWidth(2);
        mVerticalPaint.setColor(0xFF999999);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setStrokeWidth(2);
        textPaint.setTextSize(30);
        textPaint.setColor(0xFF999999);
        textPaint.setTextAlign(Paint.Align.CENTER);

        mValuePaint = new Paint();
        mValuePaint.setAntiAlias(true);
        mValuePaint.setStyle(Paint.Style.STROKE);
        mValuePaint.setStrokeWidth(3);
        mValuePaint.setColor(0xFFFE7F1A);
    }

    /**
     * 测量高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        //默认宽高;
        defaultValue = 150;
        switch (mode) {
            case MeasureSpec.AT_MOST:
                //最大值模式
                int value = Math.min(size, defaultValue);
                return value;
            case MeasureSpec.EXACTLY:
                Log.i("test","size = "+size);
                return size;
        }
        return size;
    }

    /**
     * 测量宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        //默认宽高;
        defaultValue = Integer.MAX_VALUE;
        switch (mode) {
            case MeasureSpec.AT_MOST:
                //最大值模式
                int value = Math.min(size, defaultValue);
                return value;

            case MeasureSpec.EXACTLY:
                Log.i("test","size = "+size);
                return size;

        }
        return size;

    }

    private void initSize(){
        Log.e("test","getHeight = " + getHeight());
        mStartY = getHeight() / 2;
        mWidth = getWidth();
    }

    private void smoothScrollBy(int dx, int dy) {
//        scroller.startScroll(getScrollX(), getScrollY(), -dx, 0, 500);
        mScrollX = getScrollX();
        mScrollY = getScrollY();
        mScroller.fling(getScrollX(), getScrollY(), dx, 0, 0, mWidth*5, 0, 0,mWidth*5/2,0);
//        invalidate();
        postInvalidateOnAnimation();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
        initSize();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(0,mStartY,mWidth*100,mStartY,mLinePaint);
        canvas.drawLine(mWidth / 2, mStartY, mWidth / 2,0, mValuePaint);
        Log.e("test","当前值为" + (-lastMoveX + mWidth / 2 + DEFAULT_SAPCE/2) / DEFAULT_SAPCE + "原值为" + (-lastMoveX + mWidth / 2));

//        for (int start = 0; start < mWidth; start++) {
//            int top = mStartY - 10;
//            //lastMoveX变成负的是因为向左画为负 实际意义向左画为增大
//            //lastMoveX+start是因为从屏幕左侧开始为0加lastMoveX为实际数值
//            if ((-lastMoveX + start) % (13 * 10) == 0) {
//                top = top - 20;
//                isDrawText = true;
//            } else {
//                isDrawText = false;
//            }
//            if ((-lastMoveX + start) % 13 == 0) {
//                if ((-lastMoveX + start) >= 0 && (-lastMoveX + start) <= 20000 * 13) {
//                    canvas.drawLine(start, mStartY, start, top, mVerticalPaint);
//                }
//
//            }
//
//            if (isDrawText) {
//                if ((-lastMoveX + start) >= 0 && (-lastMoveX + start) <= 20000 * 13)
//                    canvas.drawText((-lastMoveX + start) / 13 + "", start, top - 8, textPaint);
//            }
//        }
        for(int start = 0;start < mWidth*100;start+=DEFAULT_SAPCE){
            if(start == 0){
                canvas.drawLine(start,mStartY,start,mStartY - LONG_VERTICAL_LINE,mLinePaint);
            }

            if((start/DEFAULT_SAPCE + 1) % 10 == 0){
                canvas.drawLine(start,mStartY,start,mStartY - LONG_VERTICAL_LINE,mLinePaint);
                canvas.drawText(start/DEFAULT_SAPCE + 1 +"",start,mStartY - LONG_VERTICAL_LINE - 8,textPaint);
            }else{
                canvas.drawLine(start,mStartY,start,mStartY - SHOT_VERTICAL_LINE,mLinePaint);
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        mVelocityTracker.addMovement(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = x - mLastX;
                int dy = y - mLastY;
                scrollBy(-dx, 0);
                break;
            case MotionEvent.ACTION_UP:

                mVelocityTracker.computeCurrentVelocity(1000);
                float xVelocity = mVelocityTracker.getXVelocity();
                float yVelocity = mVelocityTracker.getYVelocity();
                int scrollX = getScrollX();
                smoothScrollBy((int)-xVelocity,(int)yVelocity);
                mVelocityTracker.clear();
                break;
        }
        mLastX = x;
        mLastY = y;
        return true;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            int oldX = mScrollX;
            int oldY = mScrollY;
            int x = mScroller.getCurrX();
            int y = mScroller.getCurrY();

            if (oldX != x || oldY != y) {
                overScrollBy(x - oldX, y - oldY, oldX, oldY, mWidth*5, 0,
                        mOverflingDistance, 0, false);
            }
            if (!awakenScrollBars()) {
                postInvalidateOnAnimation();
            }
        }
    }
}
